#include "myhttp.h"

#include <QTextCodec>
#include <QHttpMultiPart>
#include <QHttpPart>
#include <QtScript>
const int nHTTP_TIME = 10000;
MyHttp::MyHttp(QObject *parent) : QObject(parent)
{
    m_pNetworkManager = new QNetworkAccessManager(this);
    m_pNetworkReply = NULL;
    m_pTimer = new QTimer;
    connect(m_pTimer,SIGNAL(timeout()),this,SLOT(slot_requestTimeout()));//超时信号
}

void MyHttp::registerRequest(int flag,const QString &strUrl,const QByteArray& data)
{
    m_strUrl = strUrl;
    QByteArray jsonString =data;
    m_flag=flag;
    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(m_strUrl);
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->post(network_request,jsonString); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}
void MyHttp::registerDogRequest(int flag,const QString &strUrl,const QByteArray& data)
{
    m_strUrl = strUrl;
    QByteArray jsonString =data;
    m_flag=flag;
    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(m_strUrl);
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->post(network_request,jsonString); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}
void MyHttp::registerFoodRequest(int flag,const QString &strUrl,const QString& data)
{
    m_strUrl = strUrl;
    QByteArray jsonString =data.toLatin1();
    m_flag=flag;
    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(m_strUrl);
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->post(network_request,jsonString); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}
void MyHttp::queryregisterRequest(int flag,const QString &strUrl)
{
    m_strUrl = strUrl;
    m_flag=flag;
    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(m_strUrl);
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->get(network_request); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}
void MyHttp::loginUserRequest(int flag,const QString &strUrl)
{
    m_strUrl = strUrl;
    m_flag=flag;
    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(m_strUrl);
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->get(network_request); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}
void MyHttp::loginDogRequest(int flag,const QString &strUrl)
{
    m_strUrl = strUrl;
    m_flag=flag;
    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(m_strUrl);
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->get(network_request); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}
void MyHttp::loginFoodRequest(int flag,const QString &strUrl)
{
    m_strUrl = strUrl;
    m_flag=flag;
    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(m_strUrl);
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->get(network_request); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}

void MyHttp::upUserRequest(int flag, const QString &strUrl, User_Login_Infor user)
{
    m_strUrl = strUrl;
    m_flag=flag;
    m_user=user;
    QByteArray jsonString = "{\"user_Money\":\""+m_user.Money.toLatin1()+"\"}";

    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(QUrl("https://api.bmob.cn/1/classes/User/"+user.user_Id));
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->put(network_request,jsonString); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}
void MyHttp::passwordRequest(int flag, const QString &strUrl, User_Login_Infor user)
{
    m_strUrl = strUrl;
    m_flag=flag;
    m_user=user;
    QByteArray jsonString = "{\"user_Password\":\""+m_user.user_Password.toLatin1()+"\"}";

    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(QUrl("https://api.bmob.cn/1/classes/User/"+user.user_Id));
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->put(network_request,jsonString); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}

void MyHttp::upDogRequest(int flag, const QString &strUrl, Dog_information dog)
{
    m_strUrl = strUrl;
    m_flag=flag;
    m_dog=dog;
    QJsonObject json;
    json.insert("dog_Energy", m_dog.dog_Energy);
    json.insert("dog_Health", m_dog.dog_Health);
    json.insert("dog_Wasteful", m_dog.dog_Wasteful);


    QJsonDocument document;
    document.setObject(json);
    QByteArray jsonString = document.toJson(QJsonDocument::Compact);

    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(QUrl("https://api.bmob.cn/1/classes/Dog/"+dog.dog_Id));
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->put(network_request,jsonString); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}

void MyHttp::upFoodRequest(int flag, const QString &strUrl, Food_information food)
{
    m_strUrl = strUrl;
    m_flag=flag;
    m_food=food;
    QJsonObject json;
    json.insert("food_one", m_food.food1);
    json.insert("food_two", m_food.food2);
    json.insert("food_three", m_food.food3);
    json.insert("food_four", m_food.food4);
    json.insert("food_five", m_food.food5);
    json.insert("food_six", m_food.food6);
    json.insert("food_seven", m_food.food7);
    json.insert("food_eight", m_food.food8);


    QJsonDocument document;
    document.setObject(json);
    QByteArray jsonString = document.toJson(QJsonDocument::Compact);
    //    QString s="{\"food_one\":"+QString::number(10,m_food.food1)
    //            +",\"food_two\":"+QString::number(10,m_food.food2)
    //            +",\"food_three\":"+QString::number(10,m_food.food3)
    //            +",\"food_four\":"+QString::number(10,m_food.food4)
    //            +",\"food_five\":"+QString::number(10,m_food.food5)
    //            +",\"food_six\":"+QString::number(10,m_food.food6)
    //            +",\"food_seven\":"+QString::number(10,m_food.food7)
    //            +",\"food_eight\":"+QString::number(10,m_food.food8)
    //            +"}";
    //    QByteArray jsonString = s.toLatin1();

    QNetworkRequest network_request;
    network_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    network_request.setRawHeader(QByteArray("X-Bmob-Application-Id"), QByteArray("8ce0fd58632d478566f18bbe1f97871f"));
    network_request.setRawHeader(QByteArray("X-Bmob-REST-API-Key"), QByteArray("29d41c5974cf6935112cacaa625ba0a4"));
    network_request.setUrl(QUrl("https://api.bmob.cn/1/classes/Food/"+food.food_Id));
    if(strUrl.toLower().startsWith("https"))//https请求，需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        network_request.setSslConfiguration(sslConfig);
    }
    m_pNetworkReply = m_pNetworkManager->put(network_request,jsonString); //发起get请求

    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}
//请求结束
void MyHttp::slot_requestFinished()
{
    m_pTimer->stop();//关闭定时器
    QByteArray resultContent = m_pNetworkReply->readAll();
    QTextCodec* pCodec = QTextCodec::codecForName("UTF-8");
    QString strResult = pCodec->toUnicode(resultContent);
    int nHttpCode = m_pNetworkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();//http返回码
    if(nHttpCode == 200||nHttpCode==201)//成功
    {
        emit signal_requestFinished(m_flag,true,strResult);//请求成功
    }
    else
    {
        emit signal_requestFinished(m_flag,false,strResult);//请求失败
    }
    m_pNetworkReply->deleteLater();
    this->deleteLater(); //释放内存
}

//请求超时
void MyHttp::slot_requestTimeout()
{
    emit signal_requestFinished(m_flag,false,"timeout");//请求失败
    m_pNetworkReply->deleteLater();
    this->deleteLater();//释放内存
}
